import asyncio
import time
import multiprocessing

from src.common import clean_proxy
from src.project.Mcp.McpClient import McpClient
from src.project.Mcp.McpServer import McpServer

class Mcp:
    def __init__(self):
        self.mcp_server = None
        self.mcp_client = None

    def open_serve(self):
        self.mcp_server = multiprocessing.Process(target=McpServer.start,)
        self.mcp_server.start()

    def open_client(self):
        asyncio.run(McpClient().start())

        # 示例运行完成后，关闭 mcp_server
        self.mcp_server.terminate()
        self.mcp_server.join()
        print('-----> 示例启动完成，关闭示例...')

    def start(self):
        print('-----> 请关闭计算机代理，若启动后，会导致客户端连接失败...')

        clean_proxy() # 清除代理
        self.open_serve() # 先启动服务端
        time.sleep(2)
        print('-----> mcp server 启动成功，正在启动客户端中...')
        self.open_client() # 启动客户端
